import SMERouter from "sme-router"
//入口文件
import loginView from "$v/login";
import advView from "$v/adv";
import advListView from "$v/advList";
import adminListView from "$v/adminList";
import axios from "axios"
import request from "./util/request";
const router = new SMERouter("app", "html5");
window.router = router
router.route("/login", (req, res) => {
    res.render(loginView())
})
router.route("/adv", (req, res, next) => {
    next(advView({
        subRoute: res.subRoute(),
        url: req.url
    }))
})
router.route("/adv/advList", (req, res) => {
    res.render(advListView())
})
router.route("/adv/adminList", (req, res) => {
    res.render(adminListView())
    const oSaveAdminBtn = document.getElementById('saveAdminBtn');
    oSaveAdminBtn.onclick = async function () {
        const adminNameValue = document.adminForm.adminName.value.trim()
        const passwordValue = document.adminForm.password.value.trim()
        const rePasswordValue = document.adminForm.rePassword.value.trim()
        if (!adminNameValue || !passwordValue || !rePasswordValue) {
            return alert('输入不能为空')
        }
        if (passwordValue !== rePasswordValue) {
            return alert('密码输入不一致，请重新输入')
        }
        // axios发送请求，还有后端的搭建
        try {
           const result =await request.post('/addAdmin', {
                adminName: adminNameValue,
                password: passwordValue,
                regTime: new Date().toLocaleString(),
            })
            alert(result)
        } catch (e) {
            alert('请求异常'+e.message)
        }
    }
})